/**
 * 
 */
package pl.net.beercrm.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pl.net.beercrm.dao.GenericDao;
import pl.net.beercrm.model.Customer;
import pl.net.beercrm.model.Product;
import pl.net.beercrm.model.Purchase;

/**
 * @author Rafi
 * 
 */
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class PurchaseServiceImpl implements PurchaseService<Purchase, Long> {

	@Autowired
	private GenericDao<Purchase, Long> purchaseDao;

	@Autowired
	private GenericDao<Product, Long> productDao;

	@Autowired
	private GenericDao<Customer, Long> customerDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see pl.net.beercrm.service.GenericService#upsert(java.lang.Object)
	 */
	@Override
	public void upsert(Purchase object) {
		Customer customer = customerDao.findAllByProperty("customerId",
				object.getCustomer().getCustomerId()).get(0);
		Product product = productDao.findAllByProperty("productId",
				object.getProduct().getProductId()).get(0);
		
		if(customer != null && product != null) {
			object.setCustomer(customer);
			customer.getPurchases().add(object);
			customerDao.update(customer);
			
			object.setProduct(product);
			product.getPurchases().add(object);
			productDao.update(product);
		}
		
		purchaseDao.create(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pl.net.beercrm.service.GenericService#delete(java.lang.Object)
	 */
	@Override
	public void delete(Purchase object) {
		List<Purchase> persistedObjects = findByGuid(object.getPurchaseId());
		for (Purchase purchase : persistedObjects) {
			purchaseDao.delete(purchase);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pl.net.beercrm.service.GenericService#find(java.io.Serializable)
	 */
	@Override
	public Purchase find(Long id) {
		return purchaseDao.findById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pl.net.beercrm.service.GenericService#findByGuid(java.lang.String)
	 */
	@Override
	public List<Purchase> findByGuid(String guid) {
		return purchaseDao.findAllByProperty("purchaseId", guid);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pl.net.beercrm.service.GenericService#list()
	 */
	@Override
	public List<Purchase> list() {
		return purchaseDao.findAll();
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.service.PurchaseService#getCustomer(java.lang.Object)
	 */
	@Override
	public Customer getCustomer(Purchase object) {
		List<Purchase> persistedPurchases = findByGuid(object.getPurchaseId());
		Customer resultCustomer = new Customer();
		
		if(persistedPurchases.size() > 0) {
			Purchase purchase = persistedPurchases.get(0);
			resultCustomer = customerDao.findAllByProperty("customerId", purchase.getCustomer().getCustomerId()).get(0);
		}
		
		return resultCustomer;
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.service.PurchaseService#getProduct(java.lang.Object)
	 */
	@Override
	public Product getProduct(Purchase object) {
		Purchase persistedPurchase = find(object.getId());
		Product product = null;
		if(persistedPurchase != null) {
			List<Product> products = productDao.findAllByProperty("productId", persistedPurchase.getProduct().getProductId());
			if(products.size() > 0) {
				product = products.get(0);
			}
		}
		return product;
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.service.PurchaseService#getPurchaseByCustomer(pl.net.beercrm.model.Customer)
	 */
	@Override
	public List<Purchase> getPurchaseByCustomer(Customer object) {
		return purchaseDao.findAllByProperty("customer", object);
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.service.GenericService#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List<Purchase> findByProperty(String property, Object value) {
		return purchaseDao.findAllByProperty(property, value);
	}

}
